#!/bin/bash
#pProcess - process manager
#Copyright 2007,2008,2009,2010,2011,2012,2015
#Sigmund Berglund

#120728 rodin.s: adding gettext

#------------------------------
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation version 2.

#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. <http://www.gnu.org/licenses/>.
#------------------------------

VERSION=2.3.2
#define paths
PROGPATH="`dirname $0`" #Pprocess directory
[ "$PROGPATH" = "." ] && PROGPATH="`pwd`"
export PROGPATH=$PROGPATH

#gettext
export TEXTDOMAIN=pprocess
export OUTPUT_CHARSET=UTF-8

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do #check -xft
		if [ `echo $1 | grep s` ]; then SHUTDOWN=true; fi
		if [ `echo $1 | grep h` ]; then
			echo 'Options
  -s          Show Shutdown/Reboot button
  -h          show this help message
  -v          show version and exit'
  			exit
		fi
		if [ `echo $1 | grep v` ]; then echo "pProcess $VERSION"; exit; fi
		shift
		I=$[$I+1]
	done
done

echo -n > /tmp/pprocess-filter
$PROGPATH/func -filter

S='
<window title="Pprocess '$VERSION' - '$(gettext 'Process manager')'" icon-name="gtk-execute" default_height="450" default_width="700">
<hbox>
 <tree headers_visible="false" rules_hint="true" hover-selection="true">
  <label>hei</label>
  <variable>LIST</variable>
  <input>cat /tmp/pprocess-ps</input>
  <action signal="button-press-event">. '$PROGPATH'/func -action</action>
  <action signal="button-press-event">'$PROGPATH'/func -filter</action>
  <action signal="button-press-event">refresh:LIST</action>
 </tree>
 <notebook labels="'$(gettext 'Processes')'|'$(gettext 'Messages')'">
  <frame>
   <vbox tooltip-text="'$(gettext 'Use filter to show only wanted processes.
Keep field blank to show all processes.')'">
     <entry activates-default="true" is-focus="true" secondary-icon-stock="gtk-find">
      <variable>FILTER_STRING</variable>
      <default>'$(gettext 'Search text')'</default>
      <width>100</width><height>25</height>
      <action signal="key-release-event">'$PROGPATH'/func -set_filter</action>
      <action signal="secondary-icon-release">'$PROGPATH'/func -set_filter</action>
      <action condition="command_is_true([[ `echo \"$FILTER_STRING\" | grep -F \"'$(gettext 'Search text')'\"` ]] && echo true)" signal="enter-notify-event">clear:FILTER_STRING</action>
     </entry>
    <hbox>
     <button relief="2" can-default="true" has-default="true" use-stock="true" height-request="1" width-request="1">
      <action>'$PROGPATH'/func -filter</action>
      <action>refresh:LIST</action>
     </button>
    </hbox>
    <timer visible="false" interval="4">
     <action>'$PROGPATH'/func -filter</action>
     <action>refresh:LIST</action>
    </timer>
   </vbox>
   <tree tooltip-text="'$(gettext 'What should happen to the process
when click on it.')'" headers_visible="false">
    <label>a</label>
    <variable>ACTION</variable>
    <height>100</height><width>270></width>
    <item stock="gtk-cancel">'$(gettext 'End process (kill)')'</item>
    <item stock="gtk-nothing">""</item> 
    <item stock="gtk-go-up">'$(gettext 'CPU priority')' - '$(gettext 'High')'</item> 
    <item stock="gtk-remove">'$(gettext 'CPU priority')' - '$(gettext 'Normal')'</item> 
    <item stock="gtk-go-down">'$(gettext 'CPU priority')' - '$(gettext 'Low')'</item> 
    <item stock="gtk-nothing">""</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 1 - Hangup</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 2 - Interrupt</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 3 - Quit</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 9 - Kill</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 14 - Alarm</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 15 - Terminate</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 18 - Continue</item> 
    <item stock="gtk-nothing">'$(gettext 'Send signal')' 19 - Stop</item>
   </tree>'
  [ "$SHUTDOWN" = "true" ] && S=$S'
  <vbox>
  <hbox homogeneous="true"><hbox>
   <pixmap icon_size="5"><input file stock="gtk-refresh"></input></pixmap>
   <button width-request="180">
    <label>" '$(gettext 'Restart system')' "</label>
    <action>reboot</action>
   </button>
   <pixmap icon_size="5"><input file stock="gtk-refresh"></input></pixmap>
  </hbox></hbox>
  <hbox homogeneous="true"><hbox>
   <pixmap icon_size="5"><input file stock="gtk-stop"></input></pixmap>
   <button width-request="180">
    <label>" '$(gettext 'Power off')' "</label>
    <action>wmpoweroff</action>
   </button>
   <pixmap icon_size="5"><input file stock="gtk-stop"></input></pixmap>
  </hbox></hbox>
  </vbox>'
   S=$S'</frame>
  <frame>
   <text><label>'$(gettext 'Double-click to watch messages')'</label></text>
   <text><label>""</label></text>
   <tree rules_hint="true" headers_visible="false" exported_column="1">
    <variable>LOG_FILE</variable>
    <label>a|b</label>
    <item>'$(gettext 'Running processes')'|top</item>
    <item>'$(gettext 'Kernel messages')'|/var/log/messages</item>
    <item>'$(gettext 'Graphic server')'|/var/log/Xorg.0.log</item>
    <item>'$(gettext 'Kernel boot')'|/tmp/bootkernel.log</item>
    <item>'$(gettext 'Puppy boot')'|/tmp/bootsysinit.log</item>
    <action>. '$PROGPATH'/func -logfile</action>
   </tree>
   </frame>  
  </notebook>
</hbox>
</window>'
export PPROCESS="`echo "$S" | sed 's/##.*//'`" #I use double hash (##) for comments. --> as #FF0000
gtkdialog4 -p PPROCESS
